import { NestFactory } from '@nestjs/core';
import { AppModule } from '@/app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import { ConfigService } from '@nestjs/config';
import ILogger from '@/services/logger.service';
import { IExceptionFilter } from '@/core/filters/exception.filter';
import IGlobal from '@/services/global.service';
import * as path from 'path';
import './core/prototype.extend';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  app.useGlobalFilters(new IExceptionFilter());
  IGlobal.app = app;
  const configService: ConfigService = app.get(ConfigService);
  ILogger.bootstrap();
  app.enableCors();
  app.enable('trust proxy');
  app.setBaseViewsDir(path.join(__dirname, '..', 'views'));
  app.setViewEngine('hbs');
  await app.listen(configService.get('PORT', 3900), '0.0.0.0');
  const appUrl: string = await app.getUrl();
  IGlobal.appUrl = appUrl;
  ILogger.info(`Application is running on: ${appUrl}`);
}

bootstrap().catch((e) => e);
